<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
    <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" maxlength="50" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" maxlength="50" />
      </el-form-item>
      <!-- 部门负责人 -->
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <!-- 部门介绍 -->
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" maxlength="300" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addDepartments">确定</el-button>
        <el-button size="small" @click="$emit('update:showDialog', false)">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.formData.id) {
        // 处于编辑状态 排除自己外同部门不能有叫张三
        isRepeat = depts.filter(item => item.id !== this.formData.id && item.pid === this.treeNode.pid).some(item => item.name === value)
      } else {
        // 处于新增状态
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }
      isRepeat ? callback(new Error(`${value}命名重复！`)) : callback()
      // console.log(depts)
    }
    const checkCodeRepeat = async(rele, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.formData.id) {
        // 编辑状态
        isRepeat = depts.some(item => item.id !== this.formData.id && item.code === value && value)
      } else {
        isRepeat = depts.some(item => item.code === value && value)
      }
      isRepeat ? callback(`${value}编码已存在！`) : callback()
    }
    return {
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
        // pid: ''
      },
      rules: {
        name: [{ required: true, message: '必填项不能为空！', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称应在1-50位之间', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }],
        code: [{ required: true, message: '必填项不能为空！', trigger: 'blur' },
          { min: 1, max: 50, message: '负责任应在1-50位之间', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }],
        manager: [{ required: true, message: '必填项不能为空！', trigger: 'blur' }],
        introduce: [{ required: true, message: '必填项不能为空！', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍应在1-300位之间', trigger: 'blur' }]
      },
      peoples: []
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },
    addDepartments() {
      this.$refs.formData.validate(async valid => {
        if (valid) {
          // 表单验证通过
          // 判断是编辑状态还是新增状态 根据formData.id是否有
          if (this.formData.id) {
            // 处于编辑状态
            await updateDepartments(this.formData)
            this.$message.success('编辑部门成功！')
          } else {
            // 处于新增状态
            await addDepartments({ ...this.formData, pid: this.treeNode.id })
            this.$message.success('新增部门成功！')
          }
          this.$emit('delDepts')
          this.$emit('update:showDialog', false)
        }
      })
    },
    // 获取部门详情方法，提供外父组件调用
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    },
    btnCancel() {
      // 清除表单数据 确认和取消都会进入
      this.formData = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
      this.$refs.formData.resetFields() // 重置校验字段
      this.$emit('update:showDialog', false)
    }
  }

}
</script>

<style>

</style>
